package com.bitjiuyeke;

import java.sql.*;
import java.util.Scanner;

public class Demo02_Statement {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement =null;
        ResultSet resultSet = null;
        //1：加载数据库厂商提供的驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2：创建数据库连接
            connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/java116", "root", "123456");
            //3创建statement对象，执行sql语句
            statement = connection.createStatement();
            Scanner scanner= new Scanner(System.in);
            System.out.println("请输要查询的学生姓名：");
            String  inputname = scanner.next();
            //4定义sql语句
            String sql ="select  id,name,sno,age,gender,enroll_date,class_id from student where name = '"+inputname+"'";
            //5执行sql语句(增删改都用executeUpdate方法)
            //查询是executeQuery方法
            resultSet= statement.executeQuery(sql);//获取结果
            //6遍历结果集，获取数据行
            //遍历多条数据，使用while循环
//            while (resultSet.next()){//resultSet.next()返回true则有下一行数据，false则没有
//                //7获取数据列
//                int id = resultSet.getInt(1);
//                String name = resultSet.getString(2);
//                String sno = resultSet.getString(3);
//                int age = resultSet.getInt(4);
//                Boolean gender = resultSet.getBoolean(5);
//                Date enroll_date = resultSet.getDate(6);
//                int class_id = resultSet.getInt(7);
//                //8输出数据
//                System.out.println("id:"+id+",name:"+name+",sno:"+sno+",age:"+age+",gender:"+gender+",enroll_date:"+enroll_date+",class_id:"+class_id);
//            }
            //遍历一条结果集，使用if语句
            if (resultSet.next()){//resultSet.next()返回true则有下一行数据，false则没有
                //7获取数据列
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String sno = resultSet.getString(3);
                int age = resultSet.getInt(4);
                Boolean gender = resultSet.getBoolean(5);
                Date enroll_date = resultSet.getDate(6);
                int class_id = resultSet.getInt(7);
                //8输出数据
                System.out.println("id:"+id+",name:"+name+",sno:"+sno+",age:"+age+",gender:"+gender+",enroll_date:"+enroll_date+",class_id:"+class_id);
            }

        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            //9关闭资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
